package models.sys;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import models.cms.Post;
import play.db.jpa.Model;

import com.google.gson.annotations.Expose;

@Entity(name = "u_user")
public class User extends Model {

    public String account;
    public String nickname;
    @Column(unique = true)
    public String mobile;
    @Expose(serialize = false)
    public String validateCode;
    public Date createdAt;
    @Expose(serialize = false)
    public String salt;
    @Expose(serialize = false)
    public String passwd;
    @OneToMany
    @Expose(serialize = false)
    public List<UserInfo> infos;
    @OneToMany
    @Expose(serialize = false)
    public List<UserAccount> accounts;
    @ManyToMany
    @Expose(serialize = false)
    public List<Role> roles;

    public Date validateExpr;

    public String memo;
    public String profession;

    public static boolean existsMobile(String mobile) {
        return User.count("byMobile", mobile) > 0;
    }

    public static User findByMobile(String mobile) {
        return User.find("byMobile", mobile).first();
    }

    public long count_posts() {
        return Post.count("user = ?", this);
    }

    public List<Post> load_posts(Post post) {
        return Post.find("select p from models.cms.Post p where user = ? and to_post is null and p != ?", this, post).fetch(9);
    }
}
